package com.yhg.qxmall.product.front;

import com.yhg.common.utils.R;
import com.yhg.qxmall.product.entity.BrandEntity;
import com.yhg.qxmall.product.entity.CategoryEntity;
import com.yhg.qxmall.product.entity.SkuInfoEntity;
import com.yhg.qxmall.product.entity.SpuInfoEntity;
import com.yhg.qxmall.product.service.BrandService;
import com.yhg.qxmall.product.service.CategoryService;
import com.yhg.qxmall.product.service.SkuInfoService;
import com.yhg.qxmall.product.service.SpuInfoService;
import com.yhg.qxmall.product.vo.Catelog2Vo;
import com.yhg.qxmall.product.vo.to.OrderSpuInoVo;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("product/category")
public class FrontIndexController {
    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redisson;

    @Autowired
    SkuInfoService skuInfoService;
    @Autowired
    SpuInfoService spuInfoService;
    @Autowired
    BrandService brandService;

    @GetMapping("/categoryLevel1")
//    @ResponseBody
    public R indexPage(){
        //查出所有一级分类
        List<CategoryEntity> categoryEntities =categoryService.getLevel1Categorys();
        return R.ok().put("categorys",categoryEntities);
    }

    @GetMapping("/catelog.json")
    @ResponseBody
    public Map<String , List<Catelog2Vo>> getCatalogJson(){
        Map<String , List<Catelog2Vo>> catalogJson = categoryService.getCatalogJson();
        return catalogJson;
    }

    /******************************        =====    重写接口    =========    ************************************************/
    @GetMapping("/categoryLevelOne")
    public R productPage(){
        //查出所有一级分类
        List<CategoryEntity> categoryEntitie = categoryService.getCategoryLevelOne();
        return R.ok().put("data",categoryEntitie);
    }

    @GetMapping("/threeLevel")
    public Map<String, List<Catelog2Vo>> threeLevel(){

        Map<String, List<Catelog2Vo>> threeLevelJson  = categoryService.getthreeLevelJson();

        return threeLevelJson;
    }


    @GetMapping("/getSpuInfo/{skuId}")
    public OrderSpuInoVo getSpuInfoVos(@PathVariable("skuId") Long skuId ){
        OrderSpuInoVo orderSpuInoVo =new OrderSpuInoVo();

        SkuInfoEntity skuInfoEntity = skuInfoService.getById(skuId);
        Long spuId = skuInfoEntity.getSpuId();
        orderSpuInoVo.setSpuId(spuId);

        Long brandId = skuInfoEntity.getBrandId();
        BrandEntity brandEntity = brandService.getById(brandId);
        orderSpuInoVo.setSpuBrand(brandEntity.getName());

        SpuInfoEntity spuInfoEntity = spuInfoService.getById(spuId);
        orderSpuInoVo.setSpuName(spuInfoEntity.getSpuName());

        return orderSpuInoVo;
    }

    /******************************        =====    重写接口    =========    ************************************************/

}
